package com.example.orderservice_1.utils;

import com.example.orderservice_1.domain.Order;
import com.example.orderservice_1.mapper.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class OrderScheduler {
    @Autowired
    private OrderRepository orderRepository;

    @Scheduled(fixedRate = 60000) // 每分钟执行一次
    public void cancelUnpaidOrders() {
        List<Order> orders = orderRepository.findByStatus("CREATED");
        for (Order order : orders) {
            if (order.getCreatedAt().getTime() + 30 * 60 * 1000 < new Date().getTime()) { // 30分钟未支付
                order.setStatus("CANCELLED");
                orderRepository.save(order);
            }
        }
    }
}